package suanfa.datastructure.stack;

import java.util.EmptyStackException;

public class SeqStack<T> {
    private T[] arr;
    private int capacity=10;//指向栈底的指针
    private int top=-1;//指向栈顶的指针，初始值：-1
    private int size;

    public int size(){
        return size;
    }
    public SeqStack(int capacity){
        arr=(T[])new Object[capacity];
        size=0;
    }
    public SeqStack(){
        arr=(T[])new Object[this.capacity];
        size=0;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public void resize(int n){
        if(n< arr.length){
            return;
        }
        T[] old=arr;
        arr=(T[])new Object[n];
        for(int i=0;i<size;i++){
            arr[i]=old[i];
        }
    }
    //入栈
    public void push(T data){
        if(size==arr.length){
            resize(arr.length*2+1);
        }
        System.out.println(data+" 入栈");
        arr[++top]=data;
        size++;
    }
    public T pop() throws EmptyStackException{
        if(isEmpty()){
            throw new EmptyStackException();
        }
        size--;
        return arr[top--];
    }

//    public void daoxu(T[] a){//倒序存
//        for(int i=a.length-1;i>=0;i--){
//            push(a[i]);
//        }
//    }
    public T peek() throws EmptyStackException{
        if(isEmpty()){
            throw new EmptyStackException();
        }
        return arr[top];
    }

    public static void main(String[] args) {
        Integer  a[]={134,456,12,78,90,22};
        SeqStack<Integer> s=new SeqStack<>(a.length);

        for(int i=0;i<a.length;i++){
            s.push(a[i]);
        }
        int size=s.size;
        System.out.println("size: "+size);
        for(int i=0;i<a.length;i++){
            System.out.println("s.pop-> "+s.pop());
        }
    }
}
